<template>
	<view class="view">
		<view class="list">
			<view class="item" v-for="(item, index) in list" :key="index"
				@click="to('/pages/CommunityPages/details/main?id=' + item.id)">
				<view class="top">
					<view>
						<image class="avatar" :src="item.avatar || '/static/images/default_avatar.png'"
							mode="aspectFill">
						</image>
					</view>
					<view class="name-box">
						<p class="name">{{item.nickName || ''}}</p>
						<p class="time">{{item.createTime}}</p>
					</view>
					<view class="solution" v-if="item.helpStatus == 1">已解决</view>
					<image class="delete" src="/static/images/ic_delete.png" @click.stop="clickDelete(item.id)"></image>
				</view>
				<div v-if="item.recourseAmount > 0" class="reward">悬赏{{item.recourseAmount}}积分</div>
				<view class="content-box">
					<text class="good" v-if="item.publishStatus == 2">精</text>
					<text class="stick" v-if="item.publishStatus == 1">顶</text>
					<text class="ask" v-if="item.publishType == 1">求</text>
					<text v-if="item.publishSubject" class="title">【{{item.publishSubject}}】</text>
					<text class="content">{{item.publishContent || ''}}</text>
				</view>
				<view v-if="item.showImage" class="img-box" @click.stop>
					<img class="img" v-for="(x, y) in item.imgList" :key="y" :src="x" @click="preview(x, item.imgList)">
				</view>
				<view class="btn-primary" v-if="item.imgList.length > 0" @click.stop="changeImageStatus(index)">
					{{item.showImage ? '隐藏图片' : '查看图片'}}
				</view>
				<view class="bottom">
					<view class="disease"
						@click.stop="to('/pages/CommunityPages/circleDetails/main?id=' + item.groupId)">
						{{item.groupName}}
					</view>
					<view class="right-box">
						<view class="operation">
							<image class="icon" src="/static/images/ic_comment.png">
								<p class="count">{{item.commentsAmount || 0}}</p>
						</view>
						<view class="operation">
							<image class="icon" src="/static/images/ic_like.png">
								<p class="count">{{item.collectQuantity || 0}}</p>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getListByUser
	} from "@/apis/request";

	export default {
		data() {
			return {
				finished: false,
				pageNo: 1,
				total: 0,
				list: []
			};
		},
		onLoad() {
			this.init();
		},
		onPullDownRefresh() {
			this.init();
		},
		onReachBottom() {
			this.getUserlist();
		},
		methods: {
			init() {
				this.finished = false;
				this.pageNo = 1;
				this.list = [];
				this.getUserlist();
			},
			async getUserlist() {
				if (!this.finished) {
					const data = await getListByUser({
						pageNo: this.pageNo++,
						pageSize: 10
					});
					if (data.records.length == 0) {
						this.finished = true;
					}
					this.list = this.list.concat(
						...data.records.map(item => {
							return {
								...item,
								showImage: false,
								imgList: item.imageUrl ? item.imageUrl.split(",") : []
							};
						})
					);
					uni.stopPullDownRefresh();
				}
			},
			preview(current, imgList) {
				this.$Imgpreview(current, imgList);
			},
			changeImageStatus(index) {
				this.list[index].showImage = !this.list[index].showImage
			},
			to(url) {
				uni.navigateTo({
					url
				});
			},
			clickDelete(id) {
				const that = this
				uni.showModal({
					title: '提示',
					content: '您确定删除该帖子吗？',
					success(res) {
						if (res.confirm) {
							that.$api.http('/group/publishInfo/delete/' + id, 'DELETE', {
									id
								},
								res => {
									that.init();
								})
						}
					}
				})
			}
		}
	};
</script>

<style lang="scss" scoped>
	.view {
		width: 100%;
		background-color: #f1efef;

		.list {
			padding: 0 30rpx;
			box-sizing: border-box;
			background-color: #FFF;

			.item {
				padding: 20rpx 0;
				box-sizing: border-box;
				border-top: 1px solid #f8f8f8;

				.top {
					position: relative;
					display: flex;
					align-items: center;
					align-content: center;

					.avatar {
						width: 90rpx;
						height: 90rpx;
						border-radius: 50%;
					}

					.name-box {
						margin-left: 15rpx;
						display: flex;
						align-items: center;
						align-content: center;
						flex-direction: column;

						.name {
							width: 100%;
							font-size: 28rpx;
							text-align: left;
						}

						.time {
							margin-top: 5rpx;
							color: #aaa;
							font-size: 24rpx;
						}
					}

					.solution {
						position: absolute;
						right: 0;
						color: #FF290B;
						font-size: 26rpx;
						font-weight: bold;
						transform: rotate(45deg);
					}

					.delete {
						position: absolute;
						top: 0;
						right: 0;
						width: 28.5rpx;
						height: 33.75rpx;
					}
				}

				.reward {
					margin: 5px 0;
					font-size: 28rpx;
					color: #E5AE54;
				}

				.content-box {
					.ask {
						margin-right: 5rpx;
						padding: 0 5rpx;
						color: #F83F06;
						font-size: 24rpx;
						border: 1rpx solid #F83F06;
						background-color: #FFF3F4;
						border-radius: 5rpx
					}

					.stick {
						margin-right: 5rpx;
						padding: 0 5rpx;
						color: #1DDA61;
						font-size: 24rpx;
						border: 1rpx solid #1DDA61;
						background-color: #E6F9ED;
						border-radius: 5rpx
					}

					.good {
						margin-right: 5rpx;
						padding: 0 5rpx;
						color: #07A0F8;
						font-size: 24rpx;
						border: 1rpx solid #07A0F8;
						background-color: #E7F4FB;
						border-radius: 5rpx
					}

					.title {
						color: #333333;
						font-size: 30rpx;
					}

					.content {
						color: #333333;
						font-size: 30rpx;
					}
				}

				.img-box {
					display: flex;
					flex-wrap: wrap;

					.img {
						margin: 10rpx 10rpx 0 0;
						width: 222rpx;
						height: 222rpx;
						border-radius: 10rpx;

						&:nth-child(3n) {
							margin-right: 0;
						}
					}
				}

				.bottom {
					margin-top: 20rpx;
					display: flex;
					align-items: center;
					align-content: center;
					justify-content: space-between;

					.disease {
						padding: 10rpx 25rpx;
						color: #F48712;
						font-size: 26rpx;
						border-radius: 30rpx;
						background-color: #FEF5EE;
					}

					.right-box {
						display: flex;
						align-items: center;
						align-content: center;

						.operation {
							display: flex;
							align-items: center;
							align-content: center;

							.icon {
								width: 40rpx;
								height: 40rpx;
							}

							.count {
								margin-left: 8rpx;
								font-size: 24rpx;
							}

							&:last-child {
								margin-left: 30rpx;
							}
						}
					}
				}
			}
		}

		.btn-primary {
			height: 60rpx;
			width: 200rpx;
			margin: 0 auto;
			margin-top: 20rpx;
			line-height: 60rpx;
			text-align: center;
			background: #32C59A;
			color: #ffffff;
			font-size: 28rpx;
			border-radius: 100rpx;
		}
	}
</style>